/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.chinapay;

import java.io.File;

import org.grayrabbit.pay.chinapay.config.ChinaPayConfigUtil;
import org.grayrabbit.pay.chinapay.config.ChinaPayInfo;
import org.grayrabbit.pay.core.model.enums.PayResultEnum;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.grayrabbit.pay.utils.FileUtil;

import chinapay.PrivateKey;

/**
 * 银联基础创建公共类
 * 
 * @author wy
 * @version $Id: PayBuild.java, v 0.1 2012-6-1 下午1:16:07 wy Exp $
 */
public class PayBuild {

    /**
     * 创建公/私钥对象buildKey
     * 
     * @param keyName key文件名称
     * @param merId 私钥为商户Id，公钥为 15个9
     * @return
     */
    private static PrivateKey build(String merId,String keyName) {
        File file = new File(FileUtil.getClassPath() + keyName);
        if (!file.exists()) {
            throw new PayException(PayResultEnum.CHINAPAY_KEY_NOT_EXISTS);
        }
        PrivateKey key = new PrivateKey();
        boolean flag = key.buildKey(merId, 0,
            FileUtil.getClassPath() + keyName);
        if (!flag) {
            throw new PayException(PayResultEnum.CHINAPAY_BUILD_KEY_ERROR);
        }
        return key;
    }

    /**
     * 创建私钥对象buildKey
     * 
     * @return
     */
    public static PrivateKey buildMer() {
        return build(ChinaPayConfigUtil.getChinaPayInfo().getMemId(),ChinaPayInfo.MERPR_KEY);
    }

    /**
     * 创建公钥对象buildKey
     * 
     * @return
     */
    public static PrivateKey buildPub() {
        return build(ChinaPayInfo.PGPUBK_KEY_ID,ChinaPayInfo.PGPUBK_KEY);
    }
    
}
